package cn.demomaster.quickdb.model;

public class ColumnInfo {
    private String columnName;
    private String columnType;
    private String fieldName;
    private String fieldType;
    private boolean isPrimaryKey;
    private boolean isAutoIncrement;
    private boolean isNotNull;
    private String defaultValue;
    private String extra;
    private String columnComment;
    private boolean unique;
    private String valueFormat;

    public ColumnInfo() {
    }

    public ColumnInfo(String columnName, String columnType, String fieldName, String fieldType, boolean isPrimaryKey, boolean isAutoIncrement, String defaultValue, String columnComment,boolean isNotNull, boolean unique, String valueFormat) {
        this.columnName = columnName;
        this.columnType = columnType;
        this.fieldName = fieldName;
        this.fieldType = fieldType;
        this.isPrimaryKey = isPrimaryKey;
        this.isAutoIncrement = isAutoIncrement;
        this.defaultValue = defaultValue;
        this.columnComment = columnComment;
        this.isNotNull = isNotNull;
        this.unique = unique;
        this.valueFormat = valueFormat;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnType() {
        return columnType;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    public boolean isPrimaryKey() {
        return isPrimaryKey;
    }

    public void setPrimaryKey(boolean primaryKey) {
        isPrimaryKey = primaryKey;
    }

    public boolean isAutoIncrement() {
        return isAutoIncrement;
    }

    public void setAutoIncrement(boolean autoIncrement) {
        isAutoIncrement = autoIncrement;
    }

    public String getDefaultValue() {
        return defaultValue;
    }
    public String getDefaultValueForSql() {
        if(defaultValue!=null&& !defaultValue.isEmpty()){
            switch (columnType){
                case "INTEGER":
                    return "DEFAULT "+defaultValue;
                case "TEXT":
                    return "DEFAULT '"+defaultValue+"'";
                case "REAL":
                    return "DEFAULT "+defaultValue;
                default:
                    return "";
            }
        }else if(defaultValue==null){
            return "";
        }
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public String getColumnComment() {
        return columnComment;
    }

    public void setColumnComment(String columnComment) {
        this.columnComment = columnComment;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public boolean isUnique() {
        if(isPrimaryKey){
            unique = true;
            return true;
        }
        return unique;
    }

    public void setUnique(boolean unique) {
        this.unique = unique;
    }

    public boolean isNotNull() {
        if(isPrimaryKey){
            isNotNull = true;
            return true;
        }
        return isNotNull;
    }

    public void setNotNull(boolean notNull) {
        isNotNull = notNull;
    }

    public String getValueFormat() {
        return valueFormat;
    }

    public void setValueFormat(String valueFormat) {
        this.valueFormat = valueFormat;
    }

    /**
     * 生成类型定义
     * @return
     */
    public String generateColumnDefinition() {
        StringBuilder sb = new StringBuilder();

        // 添加列名和数据类型
        sb.append(columnName).append(" ").append(columnType);

        // 添加 NOT NULL 约束
        if (!isNotNull) {
            sb.append(" NOT NULL");
        }

        // 添加 PRIMARY KEY 约束
        if (isPrimaryKey) {
            sb.append(" PRIMARY KEY");

            // 如果是 INTEGER 类型并且设置了 AUTOINCREMENT，则添加 AUTOINCREMENT
            if ("INTEGER".equalsIgnoreCase(columnType) && isAutoIncrement) {
                sb.append(" AUTOINCREMENT");
            }
        }

        // 添加 UNIQUE 约束
        if (unique) {
            sb.append(" UNIQUE");
        }

        // 添加 DEFAULT 约束
        if (defaultValue != null && !defaultValue.trim().isEmpty()) {
            sb.append(" DEFAULT ").append(defaultValue);
        }

        return sb.toString();
    }


}
